package com.giaothong.ui;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.giaothong.R;
import com.giaothong.config.Define;
import com.giaothong.config.Define.StoreAcount;
import com.giaothong.controller.AuthenticationController;
import com.giaothong.http.core.HttpAsyncCallback;
import com.giaothong.http.core.HttpConnectionId;
import com.giaothong.http.core.HttpResult;
import com.giaothong.model.ModelLoginRegisterRequest;
import com.giaothong.ui.base.BaseActivity;
import com.giaothong.utils.DialogUtil;
import com.giaothong.utils.SessionData;

public class LoginActivity extends BaseActivity implements HttpAsyncCallback<JSONObject> {
	private EditText edtUsernameId, edtPasswordId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_login);
		init();
	}

	/**
	 * init View id
	 */
	private void init() {
		edtPasswordId = (EditText) findViewById(R.id.edtPasswordId);
		edtUsernameId = (EditText) findViewById(R.id.edtUsernameId);
		((TextView) findViewById(R.id.txtTitleBar)).setText(R.string.login);

		if (!sharePreferenceAdapter.get(StoreAcount.USER_NAME, "").equals("")) {
			edtUsernameId.setText(sharePreferenceAdapter.get(StoreAcount.USER_NAME, ""));
			edtPasswordId.setText(sharePreferenceAdapter.get(StoreAcount.PASSWORD, ""));
			AuthenticationController.loginRequest(LoginActivity.this, LoginActivity.this, sharePreferenceAdapter.get(StoreAcount.USER_NAME, ""), sharePreferenceAdapter.get(StoreAcount.PASSWORD, ""));
		}

	}

	/********************************
	 * OnClick Method
	 *******************************/
	/**
	 * Top bar Next onClick
	 * 
	 * @param view
	 */
	public void onClickTopBarNext(View view) {
		MainMapActivity.show(LoginActivity.this);
	}
	/**
	 * onClick
	 * 
	 * @param view
	 */
	public void onClick(View view) {
		switch (view.getId()) {
		// Login button click
		case R.id.btnLogin: {
			if (!edtPasswordId.getText().toString().equals("") || !edtUsernameId.getText().toString().equals("")) {
			AuthenticationController.loginRequest(LoginActivity.this, LoginActivity.this, edtUsernameId.getText().toString(), edtPasswordId.getText().toString());
			}
			break;
		}
		case R.id.btnRegister: {
			RegisterActivity.show(LoginActivity.this);
			break;
		}
		default:
			break;
		}
	}

	/**
	 * call this Activity
	 * 
	 * @param context
	 */
	public static void show(Context context) {
		Intent intent = new Intent(context, LoginActivity.class);
		context.startActivity(intent);
	}

	/**
	 * process response from server
	 */
	public void onTaskComplete(HttpConnectionId requestCode, HttpResult resultCode, JSONObject result) {
		DialogUtil.showErrorDialogIfRequestFailed(this, resultCode);
		if (resultCode == HttpResult.SUCCESS) {
			switch (requestCode) {

			// response from server
			case LOGIN_REQUEST:{
				ModelLoginRegisterRequest modelLoginRequest = new ModelLoginRegisterRequest(result);
				if (modelLoginRequest.getCode() == Define.CODE_SUCCESSFULL) {

					// store user infor
					SessionData.setUserId(modelLoginRequest.getUserEntity().getUserId());
					SessionData.setUserType(modelLoginRequest.getUserEntity().getType());
					sharePreferenceAdapter.save(StoreAcount.USER_NAME, modelLoginRequest.getUserEntity().getUserName());
					sharePreferenceAdapter.save(StoreAcount.PASSWORD, edtPasswordId.getText().toString());
					MainMapActivity.show(LoginActivity.this);
				} else {
					DialogUtil.showDialog(LoginActivity.this, R.string.error, R.string.error_login);
				}
				break;
			}
			// default
			default: {
				// do nothing
				break;
			}
			} // end switch
		}
	}

}
